﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Deparment_MessgeList : System.Web.UI.Page
{
    public string TableName, strFields, strConditions, mKey, strAccount_TYPE, strAccount_AC_SORT;
    private long strAccount_ID;
    public int Pagesize, PageIndex, PageCount;
    string strSql, delWithinInfoId;
    private System.Data.DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            strAccount_TYPE = _Account.Type;
            strAccount_ID = _Account.AccountId;
            strAccount_AC_SORT = _Account.AC_SORT;

            if (Request.Form["btnSubmit"] == "删除")
            {
                DeleteInfo();
            }
            ShowDate();
        }
    }


    /// <summary>
    /// 删除信息
    /// </summary>
    private void DeleteInfo()
    {
        int dlLength = DlWithinInfo.Items.Count;
        for (int i = 0; i < dlLength; i++)
        {
            string delWithinInfoId = ((HtmlInputCheckBox)DlWithinInfo.Items[i].FindControl("chk")).Value;
            bool isDisabled = ((HtmlInputCheckBox)DlWithinInfo.Items[i].FindControl("chk")).Disabled;
            bool isChecked = ((HtmlInputCheckBox)DlWithinInfo.Items[i].FindControl("chk")).Checked;

            string delids = "";
            if (!isDisabled && isChecked)
            {
                if (delids == "")
                {
                    delids = delWithinInfoId;
                }
                else
                {
                    delids += "," + delWithinInfoId;
                }
            }
            if (delids != "")
            {
                strSql = "delete from WithinInfo where WithinInfoId in (" + delids + ")";
                Db db = new Db();
                db.Command(strSql);
                db.Close();
            }
        }
    }


    public void ShowDate()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "WithinInfo left join v_account on v_account.accountid=WithinInfo.accountid";                                               //要查询的表明
        dbPage.Key = "WithinInfoId";                                               //表的主键    
        dbPage.Where = fctConditions();                                           //查询条件    
        dbPage.SelectField = "WithinInfoid,title,adddate,isnull(v_account.dwname,'') as accountname,isnull(AC_SORT,'') as AC_SORT,isnull(type,'') as type";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "WithinInfoId ";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dt = dbPage.DataTable(60);
        DlWithinInfo.DataSource = dt;
        DlWithinInfo.DataBind();

        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", Title;
        Title = txtTitle.Text.ToString();

        string strType = strAccount_TYPE == "15" ? "1" : "15";  //市招办只能看到自己和省招办发的内部信息 省招办能看到自己和市招办发的信息
        if (strAccount_AC_SORT == "1")
        {
            strtemp = "  info_sort=1 "; //内部信息
        }
        else
        {
            strtemp = "  info_sort=1 and (v_account.AC_SORT=" + strAccount_AC_SORT + " or  v_account.type=" + strType + ")";
        }
        if (Title != "")
        {
            strtemp += " and Title like '%" + Title + "%' ";
        }


        return strtemp;
    }

    #region IPageDescribe 成员

    public string Name
    {
        get { return "中心内部信息"; }
    }

    public string Describe
    {
        get { return "中心内部信息"; }
    }

    public string ParentName
    {
        get { return "中心内部信息"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumDeparment);
             return _AccountType;
        }
    }

    #endregion

    protected void DlWithinInfo_ItemCreated(object sender, DataListItemEventArgs e)
    {

        if (dt != null && e.Item != null && e.Item.ItemIndex != -1)
        {

            string AC_SORT = dt.Rows[e.Item.ItemIndex]["AC_SORT"] == null ? "" : dt.Rows[e.Item.ItemIndex]["AC_SORT"].ToString();
            string type = dt.Rows[e.Item.ItemIndex]["type"] == null ? "" : dt.Rows[e.Item.ItemIndex]["type"].ToString();
            if (AC_SORT == strAccount_AC_SORT && type == strAccount_TYPE)
            {
                ((HtmlInputCheckBox)e.Item.FindControl("chk")).Disabled = false;
                ((HyperLink)e.Item.FindControl("tt")).NavigateUrl = "Deparment_MessgeUpdate.aspx?id=xx&WithinInfoid=" + SysFun.PasswordEncrypt(dt.Rows[e.Item.ItemIndex]["WithinInfoId"].ToString());
            }
            else
            {
                if (strAccount_TYPE == "1")
                {
                    ((HtmlInputCheckBox)e.Item.FindControl("chk")).Disabled = false;
                }
                else
                {
                    ((HtmlInputCheckBox)e.Item.FindControl("chk")).Disabled = true;
                }
                ((HyperLink)e.Item.FindControl("tt")).NavigateUrl = "Deparment_Messge.aspx?id=xx&WithinInfoid=" + SysFun.PasswordEncrypt(dt.Rows[e.Item.ItemIndex]["WithinInfoId"].ToString());
            }
        }
    }
}